package com.shipxpress.register.web;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.shipxpress.SessionManager;
import com.shipxpress.register.domain.User;
import com.shipxpress.register.util.UserList;

public class UserRegistrationAction extends ActionSupport{

    private User user;

    @Override
    public String execute() throws Exception {

       //populate values to User and do business logic operations
        Map sessionMap = ActionContext.getContext().getSession();
        UserList userList = SessionManager.getSessionUserList(sessionMap);
        userList.add(user);
        SessionManager.setUserList(sessionMap, userList);
        SessionManager.setRecentUser(sessionMap, user);
        return SUCCESS;
    }

    @Override
    public void validate() {

        if (user.getName() == null || user.getName().trim().length() < 1) {
            addFieldError("user.name", getText("error.name.required"));
        }
        if (user.getAddress() == null || user.getAddress().length() < 1) {
             addFieldError("user.address", getText("error.address.required"));
        }
        if (user.getEmailAddress() == null || user.getEmailAddress().length() < 1) {
             addFieldError("user.emailAddress", getText("error.emailaddress.required"));
        }
        if (user.getAge() == null || user.getAge() < 1) {
             addFieldError("user.age", getText("error.age.required"));
        }
    }


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
